﻿using BLL;
using DataTables.Mvc;
using Inspinia_MVC5.Code;
using Inspinia_MVC5.Models;
using Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AllowCors]
    [SessionTimeOutFilter]
    public class ReportController : Controller
    {
        // GET: Report
        public ActionResult WellReport()
        {
            return View();
        }
        public ActionResult BlockReport()
        {
            return View();
        }
        //排序ReportDataModel
        public List<ReportDataModel> sortReportData(string sortkey, List<ReportDataModel> ReportDataModel)
        {
            switch (sortkey)
            {
                case "BlockId desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.BlockId).ToList();
                    break;
                case "BlockId asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.BlockId).ToList();
                    break;
                case "WellId desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.WellId).ToList();
                    break;
                case "WellId asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.WellId).ToList();
                    break;
                case "XingHao desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.XingHao).ToList();
                    break;
                case "XingHao asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.XingHao).ToList();
                    break;
                case "CengWei desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.CengWei).ToList();
                    break;
                case "CengWei asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.CengWei).ToList();
                    break;
                case "Time desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.Time).ToList();
                    break;
                case "Time asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.Time).ToList();
                    break;
                case "ChongCheng desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.ChongCheng).ToList();
                    break;
                case "ChongCheng asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.ChongCheng).ToList();
                    break;
                case "ChongCi desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.ChongCi).ToList();
                    break;
                case "ChongCi asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.ChongCi).ToList();
                    break;
                case "TaoYa desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.TaoYa).ToList();
                    break;
                case "TaoYa asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.TaoYa).ToList();
                    break;
                case "HuiYa desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.HuiYa).ToList();
                    break;
                case "HuiYa asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.HuiYa).ToList();
                    break;
                case "WenDu desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.WenDu).ToList();
                    break;
                case "WenDu asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.WenDu).ToList();
                    break;
                case "YouLiang desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.YouLiang).ToList();
                    break;
                case "YouLiang asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.YouLiang).ToList();
                    break;
                case "ShuiLiang desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.ShuiLiang).ToList();
                    break;
                case "ShuiLiang asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.ShuiLiang).ToList();
                    break;
                case "QiLiang desc":
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.QiLiang).ToList();
                    break;
                case "QiLiang asc":
                    ReportDataModel = ReportDataModel.OrderBy(w => w.QiLiang).ToList();
                    break;
                default:
                    ReportDataModel = ReportDataModel.OrderByDescending(w => w.WellId).ToList();
                    break;
            }
            return ReportDataModel;
        }
        public ActionResult Get([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel, string time)
        {
            string wellstr = Session["wellstr"].ToString();
            List<ReportDataModel> query = ResultHelper.getReportData(wellstr,time);
            var totalcount = query.Count();

            #region Filtering
            if (requestModel.Search.Value != string.Empty)
            {
                var value = requestModel.Search.Value.Trim();
                query = query.Where(p => p.BlockId.Contains(value) ||
                                         p.WellId.Contains(value) ||
                                         p.XingHao.Contains(value) ||
                                         p.CengWei.Contains(value) ||
                                         p.JingDuan.Contains(value)
                                    ).ToList();
            }
            var filteredCount = query.Count();
            #endregion

            #region Sorting
            var sortedColumns = requestModel.Columns.GetSortedColumns();
            var orderByString = string.Empty;
            foreach (var column in sortedColumns)
            {
                orderByString += orderByString != string.Empty ? "," : "";
                orderByString += (column.Data) + (column.SortDirection == Column.OrderDirection.Ascendant ? " asc" : " desc");
            }
            query = sortReportData(orderByString, query);
            #endregion
            //Paging
            query = query.Skip(requestModel.Start).Take(requestModel.Length).ToList();

            return Json(new DataTablesResponse(requestModel.Draw, query, filteredCount, totalcount), JsonRequestBehavior.AllowGet);
        }
        public ActionResult ExportExcel(string time)
        {
            string wellstr = Session["wellstr"].ToString();
            List<ReportDataModel> query = ResultHelper.getReportData(wellstr, time);

            //string[] headers = { "区块", "井号", "型号", "层位", "井段", "生产时间", "冲程", "冲次", "套压", "汇压", "温度", "油量", "水量", "气量" };
            //string[] cellKes = { "BlockId", "WellId", "XingHao", "CengWei", "JingDuan", "Time", "ChongCheng", "ChongCi", "TaoYa", "HuiYa", "WenDu", "YouLiang", "ShuiLiang", "QiLiang" };

            //ExcelUtil.ExportByWeb(dt, "用户日志信息", headers, cellKes, "用户日志信息.xls");

            //创建Excel文件的对象
            NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            //添加一个sheet
            NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
            //获取list数据
            //给sheet1添加第一行的头部标题
            NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
            row1.CreateCell(0).SetCellValue("区块");
            row1.CreateCell(1).SetCellValue("井号");
            row1.CreateCell(2).SetCellValue("型号");
            row1.CreateCell(3).SetCellValue("层位");
            row1.CreateCell(4).SetCellValue("井段");
            row1.CreateCell(5).SetCellValue("生产时间");
            row1.CreateCell(6).SetCellValue("冲程");
            row1.CreateCell(7).SetCellValue("冲次");
            row1.CreateCell(8).SetCellValue("套压");
            row1.CreateCell(9).SetCellValue("汇压");
            row1.CreateCell(10).SetCellValue("温度");
            row1.CreateCell(11).SetCellValue("油量");
            row1.CreateCell(12).SetCellValue("水量");
            row1.CreateCell(13).SetCellValue("气量");
            //将数据逐步写入sheet1各个行
            for (int i = 0; i < query.Count; i++)
            {
                NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                rowtemp.CreateCell(0).SetCellValue(query[i].BlockId.ToString());
                rowtemp.CreateCell(1).SetCellValue(query[i].WellId.ToString());
                rowtemp.CreateCell(2).SetCellValue(query[i].XingHao.ToString());
                rowtemp.CreateCell(3).SetCellValue(query[i].CengWei.ToString());
                rowtemp.CreateCell(4).SetCellValue(query[i].JingDuan.ToString());
                rowtemp.CreateCell(5).SetCellValue(query[i].Time.ToString());
                rowtemp.CreateCell(6).SetCellValue(query[i].ChongCheng.ToString());
                rowtemp.CreateCell(7).SetCellValue(query[i].ChongCi.ToString());
                rowtemp.CreateCell(8).SetCellValue(query[i].TaoYa.ToString());
                rowtemp.CreateCell(9).SetCellValue(query[i].HuiYa.ToString());
                rowtemp.CreateCell(10).SetCellValue(query[i].WenDu.ToString());
                rowtemp.CreateCell(11).SetCellValue(query[i].YouLiang.ToString());
                rowtemp.CreateCell(12).SetCellValue(query[i].ShuiLiang.ToString());
                rowtemp.CreateCell(13).SetCellValue(query[i].QiLiang.ToString());
            }
            // 写入到客户端 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return File(ms, "application/vnd.ms-excel", "报表.xls");
        } 
    }
}